package com.zhuangfei.hos_timetableview.ability;

import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.adapter.OnDateDelayAdapter;
import com.zhuangfei.hos_timetableview.model.MySubject;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 日期延迟案例：
 * 需求：设定一个开学时间，在开学时间到来之前，一直显示开学时的第一周的日期
 */
public class DateDelayAbility extends Ability {

    // 控件
    TimetableView mTimetableView;
    Text titleTextView;
    List<MySubject> mySubjects;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_color_pool);
        // 添加表头的include
        StackLayout stInclude = (StackLayout) findComponentById(ResourceTable.Id_st_include);
        stInclude.addComponent(LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_header_base_func, stInclude, false));
        mySubjects = SubjectRepertory.loadDefaultSubjects();
        titleTextView = (Text) findComponentById(ResourceTable.Id_id_title);
        initTimetableView();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        // 用于更正日期的显示
        int cur = mTimetableView.curWeek();
        mTimetableView.onDateBuildListener().onUpdateDate(cur, cur);
        // 更新文本
        OnDateDelayAdapter adapter = (OnDateDelayAdapter) mTimetableView.onDateBuildListener();
        long when = adapter.whenBeginSchool();
        if (when > 0) {
            titleTextView.setText("距离开学还有" + when + "天");
        }
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .curTerm("大三下学期")
                .maxSlideItem(10)
                .callback(getDateDelayAdapter()) // 这行要放在下行的前边
                .callback(new ISchedule.OnWeekChangedListener() {
                    @Override
                    public void onWeekChanged(int curWeek) {
                        if (mTimetableView.onDateBuildListener() instanceof OnDateDelayAdapter) {
                            OnDateDelayAdapter adapter = (OnDateDelayAdapter) mTimetableView.onDateBuildListener();
                            long when = adapter.whenBeginSchool();
                            if (when > 0) {
                                titleTextView.setText("距离开学还有" + when + "天");
                            } else {
                                titleTextView.setText("第" + curWeek + "周");
                            }
                        }
                    }
                })
                .showView();
    }

    /**
     * 配置OnDateDelayAdapter
     *
     * @return 日期栏构建器
     */
    public OnDateDelayAdapter getDateDelayAdapter() {
        OnDateDelayAdapter onDateDelayAdapter = new OnDateDelayAdapter();
        // 计算开学时间戳
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long startTime = 0;
        try {
            startTime = sdf.parse("2018-09-03 00:00").getTime();
        } catch (ParseException e) {
            Logger.getLogger(DateDelayAbility.class.getName()).log(Level.SEVERE, e.getMessage());
        }
        // 计算开学时的一周日期，我这里模拟一下
        List<String> dateList = Arrays.asList("9", "03", "04", "05", "06", "07", "08", "09");
        // 设置
        onDateDelayAdapter.setStartTime(startTime);
        onDateDelayAdapter.setDateList(dateList);
        return onDateDelayAdapter;
    }
}
